<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Systemadmin extends CI_Controller {
	
	private $tab = 'system';
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	public function add_view(){
		$this->load->helper(array('form'));
		$this->load->model('systemadmin_model');
		$type = $this->systemadmin_model->get_systemadmin_type_select('id,name');
		$vdata['tab']  = $this->tab;
		$vdata['sub']  = 'systemadmin';
		$vdata['type'] = $type;
		$this->load->view("/ychadmin/systemadmin_user_add", $vdata);
	}

	public function edit_view($id){
		$this->load->helper(array('form'));
		$this->load->model('systemadmin_model');
		$id = intval($id);
		$vdata = $this->systemadmin_model->getone_systemadmin_user("id='$id'");
		$type  = $this->systemadmin_model->get_systemadmin_type_select('id,name');
		$vdata['tab']  = $this->tab;
		$vdata['sub']  = 'systemadmin';
		$vdata['type'] = $type;
		$vdata['id'] = $id;
		$this->load->view("/ychadmin/systemadmin_user_edit", $vdata);
	}

	/**
	 * 增加管理员
	 * 
	 */
	public function add(){
		
		$passwd = $this->input->post('password');
		
		$data = array(
			'adminname'  => $this->input->post('name'),
			'password'   => md5(md5($passwd)),
			'type_id'    => $this->input->post('admin_type'),
			'remark'     => $this->input->post('remark'),
		);

		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', '用户名', 'callback_username_check');
		if($this->input->post('admin_type')=='_selfHeart_'){
			$data['type_id'] = $this->input->post('kftype');
			$data['kefu_qq'] = $this->input->post('qq');
			$this->form_validation->set_rules('qq','QQ号码','required|trim|integer|min_length[5]|max_length[15]');
		}
	    if ($this->form_validation->run() == FALSE){
				$this->load->view('/ychadmin/systemadmin_user_add');
				return;
		}
		$data['status'] = 1;
  		$data['addtime'] = time();
		$data['addtime'] = time();
		$data['addip']   = $_SERVER['REMOTE_ADDR'];
		$this->load->model('systemadmin_model');
  		$systemset_id = $this->systemadmin_model->add($data);

  		header("location:/ychadmin/systemadmin/admin_list/");
		$this->load->model('log_model');$this->log_model->add_log('[添加管理员][系统管理]['.$data['name'].']');
   		exit;
	}

	/**
	 * 检测管理员
	 * 
	 */
	public function username_check($name){
		$this->load->model('systemadmin_model');
  		$user = $this->systemadmin_model->check_systemadmin_user($name);
		if($user == true){
		   $this->form_validation->set_message('username_check', '用户名请勿重复！系统已经存在"'.$name.'"');
		   return FALSE;
		}else{
			return TRUE;
		}
	}

	/**
	 * 修改管理员
	 * @param $id
	 */
	public function edit(){
		$passwd = $this->input->post('password');
		
		$data = array(
			//'password'   => md5(md5($passwd)),
			'remark'     => $this->input->post('remark'),
			'type_id'    => $this->input->post('admin_type'),
			'status'     => $this->input->post('fstatus')
		);

		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		if($this->input->post('admin_type')=='_selfHeart_'){
			$data['kefu_qq'] = $this->input->post('qq');
			$data['type_id'] = $this->input->post('kftype');
			$this->form_validation->set_rules('qq','QQ号码','required|trim|integer|min_length[5]|max_length[15]');
			if ($this->form_validation->run() == FALSE){
					$this->load->view('/ychadmin/systemadmin_user_edit');
					return;
			}
		}
		$data['update_time'] = time();
		$data['update_ip']   = $_SERVER['REMOTE_ADDR'];
		$this->load->model('systemadmin_model');
  		$systemset_id = $this->systemadmin_model->update($this->input->post('id'),$data);

  		header("location:/ychadmin/systemadmin/admin_list/");
		$this->load->model('log_model');$this->log_model->add_log('[修改管理员][系统管理]');
   		exit;
	}
	
	/**
	 * 管理员列表
	 * 
	 */	
	public function admin_list (){
		$per_page  = intval($this->input->get('per_page')?$this->input->get('per_page'):0);

		$data = array();
		$data['page_size']  = 10;
		$data['page_start'] = (int)$per_page;
		$this->load->model('systemadmin_model');
		$vdata = $this->systemadmin_model->get_systemadmin_user($data);
				
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/systemadmin/admin_list/?pre_page='.$per_page;
		$config['total_rows'] = $vdata['user_count'];
		$config['per_page'] = 10;
		$this->pagination->initialize($config);
		
		$type = $this->systemadmin_model->get_systemadmin_type_select('id,name');
		$vdata['admintype'] = $type;
		
		
		$vdata['tab']  = $this->tab;
		
		$vdata['sub']  = 'systemadmin';
		
		//var_dump($vdata);
		$this->load->view('/ychadmin/systemadmin_user_list', $vdata);
	}

	/**
	 * 管理员类型列表
	 * 
	 */	
	public function admin_type (){
		$this->config->load('admin_type',TRUE);
		$type = $this->config->item('admintype', 'admin_type');

		$per_page  = intval($this->input->get('per_page')?$this->input->get('per_page'):0);

		$data = array();
		$data['page_size']  = 10;
		$data['page_start'] = (int)$per_page;
		$this->load->model('systemadmin_model');
		$vdata = $this->systemadmin_model->get_systemadmin_type($data);
				
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/systemadmin/admin_type/?pre_page='.$per_page;
		$config['total_rows'] = $vdata['type_count'];
		$config['per_page'] = 10;
		$this->pagination->initialize($config); 
		
		$vdata['admintype'] = $type;
		$vdata['tab']  = 'sytemadmin_type';
		$vdata['sub']  = 'systemadmin';
		
		$vdata['tab']  = $this->tab;
		$vdata['sub']  = 'sytemadmin_type';
		//var_dump($vdata);
		$this->load->view('/ychadmin/systemadmin_type_list', $vdata);

	}

	public function admin_type_add_view(){
		$this->config->load('admin_type',TRUE);
		$type = $this->config->item('admintype', 'admin_type');
		$this->load->helper(array('form'));
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'sytemadmin_type';
		$vdata['type'] = $type;
		$this->load->view("/ychadmin/systemadmin_type_add", $vdata);
	}

	public function admin_type_edit_view($id){
		$this->config->load('admin_type',TRUE);
		$type = $this->config->item('admintype', 'admin_type');
		$this->load->helper(array('form'));
		$this->load->model('systemadmin_model');
		$id = intval($id);
		$vdata = $this->systemadmin_model->getone_systemadmin_type("id='$id'");
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'sytemadmin_type';
		$vdata['id'] = $id;
		$vdata['type'] = $type;
		$this->load->view("/ychadmin/systemadmin_type_edit", $vdata);
	}

	/**
	 * 增加管理员类型 
	 * 
	 */	
	public function admin_type_add (){
		foreach($this->input->post('module') as $v){
			  $module .=$v.",";
		}
		$module = substr($module,0,-1);
		$data = array(
			'name'    => $this->input->post('name'),
			'remark'  => $this->input->post('remark'),
			'module'  => $module
		);

		$data['status'] = 1;
		$data['addtime'] = time();
		$data['addip']   = $_SERVER['REMOTE_ADDR'];
		$this->load->model('systemadmin_model');
  		$systemset_id = $this->systemadmin_model->type_add($data);

  		header("location:/ychadmin/systemadmin/admin_type/");
		$this->load->model('log_model');$this->log_model->add_log('[添加][管理员类型]['.$data['name'].']');
   		exit;	
	}

	/**
	 * 编辑管理员类型 
	 * 
	 */	
	public function admin_type_edit (){
		foreach($this->input->post('module') as $v){
			  $module .=$v.",";
		}
		$module = substr($module,0,-1);
		$data = array(
			'name'    => $this->input->post('name'),
			'remark'  => $this->input->post('remark'),
			'status'  => $this->input->post('fstatus'),
			'module'  => $module
		);

		$data['updatetime'] = time();
		$data['updateip']   = $_SERVER['REMOTE_ADDR'];
		$this->load->model('systemadmin_model');
  		$systemset_id = $this->systemadmin_model->type_update($this->input->post('id'),$data);

  		header("location:/ychadmin/systemadmin/admin_type/");
		$this->load->model('log_model');$this->log_model->add_log('[修改][管理员类型]['.$data['name'].']');
   		exit;		
	}

	/**
	 *  删除管理员类型
	 * 
	 */	
	public function admin_type_del (){
		$this->load->model('systemadmin_model');
		$post_data = $this->input->post();
		$id = intval($post_data['id']);
		$del = $this->systemadmin_model->systemadmin_type_del($id);
		if($del) echo 1;
	}

	/**
	 *  删除管理员
	 * 
	 */	
	public function admin_user_del (){
		$this->load->model('systemadmin_model');
		$post_data = $this->input->post();
		$id = intval($post_data['id']);
		$del = $this->systemadmin_model->systemadmin_user_del($id);
		if($del) echo 1;
	}

	public function tests(){
		$this->config->load('admin_type',TRUE);
		$admintype = $this->config->item('admintype', 'admin_type');
		//print_r($type);
	    $type_arr = explode(',','first,all,statistics,account');
		$b = array();
		foreach ($admintype as $k=>$v){
			foreach($v['list'] as $lk => $lv){
				foreach($lv['sonlist'] as $sk =>$sv){
					if(array_key_exists(array_search($sk,$type_arr),$type_arr)){
						//$mlist .= $sv['linkname']."-".$lv['sonname']."-".$v['name']."<br>";
						if($k=="borrow"){
								$b[$lv['sonname']][$sk][] = $sv['linkname'];
								$b[$lv['sonname']][$sk][] = $sv['link'];

						}
					}
				}
			}
		}
		//echo substr($mlist,0,-1);
		//unset($mlist);
		print_r($b);

	}
}